package admin.controllers;

import helpers.IdUniqueHelper;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import admin.forms.SubmitSiteForm;
import admin.models.SubmitSiteModel;

import entities.SubmitSite;


@SuppressWarnings("serial")
public class SubmitSiteAddServlet extends HttpServlet 	{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		SubmitSite submitSite = new SubmitSite();
		
		request.setAttribute("check", check);
		request.setAttribute("submitSite", submitSite);
		try {
			request.getRequestDispatcher("/admin/pages/submit_site_form.jsp").forward(request, response);
		} catch (ServletException e) {
			//
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		SubmitSiteModel submitSiteModel = SubmitSiteModel.getInstance();
		SubmitSite submitSite = new SubmitSite();
		
		TreeMap<String, Object> form = SubmitSiteForm.checkValid(request, submitSite);
		submitSite = (SubmitSite) form.get("submitSite");
		check = (TreeMap<String, String>) form.get("checkValid");
		
		if (check.size() == 0) {
			submitSite.setId(IdUniqueHelper.getId());
			submitSiteModel.insert(submitSite);
			
			request.setAttribute("success", "Add success submit site with url " + submitSite.getSiteUrl());
			
			submitSite = new SubmitSite();
		}
		
		request.setAttribute("check", check);
		request.setAttribute("submitSite", submitSite);
		try {
			request.getRequestDispatcher("/admin/pages/submit_site_form.jsp").forward(request, response);
		} catch (ServletException e) {
			//
		}
	}
}
